<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全选反选</title>
</head>

<body>

    <input id='checkAll' type="checkbox" value='全选'> 全选
    <input type="checkbox" name="items" value="足球">足球
    <input type="checkbox" name="items" value="篮球">篮球
    <input type="checkbox" name="items" value="游戏">游戏
    <input type="checkbox" name="items" value="小说">小说


</body>
<script>
    var items = document.getElementsByName('items');

    var checkAllEle = document.querySelector("#checkAll");
    checkAllEle.onclick = function () {
        // console.log(this.checked);

        // var that = this;
        items.forEach(function (item) {
            // console.log(this);
            // item.checked = that.checked;
            item.checked = this.checked;
        })
        // console.log(data);
    }

    function isCheckAll(items) {
        var res = items.every(function (item) {
            return item.checked;
        })
        if (res) {
            // 全选
            checkAllEle.checked = true;
        } else {
            // 不全选 
            checkAllEle.checked = false;
        }
    }





</script>

</html>